#include "myoption.h"
#include <QPushButton>
#include <QApplication>
#include <QSize>
#include <QMediaPlayer>

MyOption::MyOption(QWidget *parent) :
    QWidget(parent)
{
    //初始化控制面板
    this->setAutoFillBackground(true);
    this->setPalette(QPalette(QColor( 255, 0, 0, 0)));
    this->resize(480, 800);
    this->show();

    //显示分数和点击显示设置
    button = new QPushButton(this);
    button->setFlat(true);
    button->setEnabled(true);
    button->setGeometry( 0, 0, 480, 200);
    button->show();
    connect( button, SIGNAL(clicked()), this, SIGNAL(pause()));

    //加载动画
    movie = new QMovie(this);
    lable = new QLabel(this);
    lable->setGeometry( 45, 280, 480, 200);
    lable->hide();

    movie->setFileName(":/resources/gameover.gif");
    movie->setCacheMode(QMovie::CacheAll);
    lable->setMovie(movie);
}

/**
 * @brief 获得场景纵坐标并作为分数显示
 *
 * @param dy 场景纵坐标为负值
 */
void MyOption::GetDy(const qreal &dy)
{
    int score = -(int)dy/100;
    QString y = QString::number(score);
    StatusValue::MyScore = score;
    button->setText(y);
}

/**
 * @brief 开始结束动画
 *
 */
void MyOption::gameOverStart()
{
    QMediaPlayer *player = new QMediaPlayer;
#ifdef WIN32
    player->setMedia(QUrl::fromLocalFile("D:/qt/test/game_over.mp3"));
#endif

#ifdef ANDROID
    player->setMedia(QUrl::fromLocalFile("/sdcard/music/game_over.mp3"));
#endif
    player->play();
    button->setEnabled(false);
    lable->show();
    movie->start();
}

/**
 * @brief 停止结束动画
 *
 */
void MyOption::gameOverStop()
{
    button->setEnabled(true);
    lable->hide();
    movie->stop();
}
